package spring.cloud.tasks.executor.listener.config;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.listener.ListenerHolder;
import spring.cloud.tasks.executor.listener.failover.FailoverService;
import spring.cloud.tasks.executor.node.TaskNodePaths;
import spring.cloud.tasks.executor.task.ExecuteService;
import spring.cloud.tasks.executor.task.TaskExecuteContextService;
import spring.cloud.tasks.executor.task.TaskExecutor;

@Slf4j
@Setter
@Getter
public class ConfigurationListenerHolder extends ListenerHolder {
    public boolean isShutdown = false;

    public TaskExecuteContextService taskExecuteContextService;

    public ExecuteService executeService;

    public FailoverService failoverService;

    public TaskConfigurationService taskConfigurationService;

    public ConfigurationListenerHolder(TaskExecutor taskExecutor) {
        super(taskExecutor);
        taskId = taskExecutor.getTaskConfiguration().getTaskId();
        taskConfiguration = taskExecutor.getTaskConfiguration();
        taskConfigurationService = taskExecutor.getTaskConfigurationService();
        //
        taskExecuteContextService = taskExecutor.getExecuteContextService();
        executeService = taskExecutor.getExecuteService();
        failoverService = taskExecutor.getFailoverService();

    }

    @Override
    public void start() {
        nodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.CRON), 0, new TaskCronUpdateListener(this));
        nodeCacheOrTreeCacheManager.addTreeCacheListener(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.ENABLED), 0, new EnabledOrDisableTaskListener(this));
    }

    @Override
    public void stop() {
        super.stop();
        isShutdown = true;
        nodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.CRON), 0);

        nodeCacheOrTreeCacheManager.closeTreeCache(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.ENABLED), 0);
    }

}
